﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace ECommerce.DAO.SQL
{
    public static class WebObjeto
    {
        public static ECommerce.WebControls.WebObjeto GetWebObjetoById(Int32 Id)
        {
            ECommerce.Model.Objeto Objeto = new ECommerce.Model.Objeto();
            Objeto.Load(Id);
            ECommerce.WebControls.WebObjeto result = new ECommerce.WebControls.WebObjeto();
            ECommerce.Factory.WebControls.WebObjeto.Load(result, Objeto);
            return result;
        }
        public static int SetWebObjeto(ECommerce.WebControls.WebObjeto WebObjeto)
        {
            ECommerce.Model.Objeto Objeto = new ECommerce.Model.Objeto();
            ECommerce.Factory.Objeto.Load(Objeto, WebObjeto);
            Objeto.Save();
            return Objeto.Id;
        }
        public static int DelObjeto(ECommerce.Model.Objeto Objeto)
        {
            int result = 0;
            using (SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["Repuestos"].ConnectionString))
            {
                using (SqlCommand sqlComm = new SqlCommand("DelObjetoById"))
                {
                    sqlComm.CommandType = CommandType.StoredProcedure;
                    SqlParameter param = new SqlParameter();
                    param.SqlDbType = SqlDbType.BigInt;
                    param.Value = Objeto.Id;
                    param.ParameterName = "@Id";
                    sqlComm.Parameters.Add(param);

                    sqlComm.Connection = sqlConn;
                    sqlConn.Open();
                    result = Convert.ToInt32(sqlComm.ExecuteScalar());
                    sqlConn.Close();
                }
            }
            return result;
        }
    }
}
